package com.face.server.controller;

import com.face.RespMsg;
import com.face.server.entity.Admin;
import com.face.server.entity.Warning;
import com.face.server.service.WarningService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 *
 * @author cong
 * @date 2018/3/27
 */

@RestController
@RequestMapping("/warning")
public class WarningController {
    @Autowired
    WarningService warningService;

    @GetMapping
    public RespMsg getWarns(@PageableDefault Pageable pageable){
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin)subject.getPrincipal();
        if(admin==null){
            return RespMsg.error("请先登录");
        }
        List<Warning> warningList = warningService.getWarns();
        return RespMsg.build(new PageImpl<>(warningList, pageable, warningList.size()));
    }

    @GetMapping("/search")
    public RespMsg getPartWarns(@PageableDefault Pageable pageable,@RequestParam(required = false) String name,@RequestParam(required = false) String begin,
                                @RequestParam(required = false) String end,@RequestParam(required = false) String time) throws ParseException {
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin)subject.getPrincipal();
        if(admin==null){
            return RespMsg.error("请先登录");
        }
        if(time ==null){
            List<Warning> warningList = warningService.getPartWarns(name,begin,end);
            return RespMsg.build(new PageImpl<>(warningList, pageable, warningList.size()));
        }else{
            List<Warning> warningList = warningService.getWarnsByTime(name,time);
            return RespMsg.build(new PageImpl<>(warningList, pageable, warningList.size()));
        }
    }


}
